﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Cfg;
using Dominio;
using NHibernate.Tool.hbm2ddl;
using System.Collections;

namespace NHibernateConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Configuration configuration = new Configuration();
            configuration.Configure();

            (new SchemaExport(configuration)).Create(true, true);

            var factory = configuration.BuildSessionFactory();

            Persona per1 = new Persona()
            {
                Apellido = "Apellido 1",
                Nombre = "Nombre 1",
            };

            Domicilio dom1 = new Domicilio()
            {
                Calle = "Calle 1"
            };

            Domicilio dom2 = new Domicilio()
            {
                Calle = "Calle 2"
            };

            per1.AddDomicilio(dom1);
            per1.AddDomicilio(dom2);

            using (var session = factory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Save(per1);
                    //session.Save(uni1);

                    //session.Save(uni2);

                    transaction.Commit();
                    //transaction.Rollback ();
                }
            }

            using (var session = factory.OpenSession())
            {
                Persona persona = session.Get<Persona>(1);

                Console.WriteLine(persona.Id );
                Console.WriteLine(persona.Apellido);
                Console.WriteLine(persona.Nombre);

                foreach (var domicilio in persona.Domicilios)
                {
                    Console.WriteLine(domicilio.Id);
                    Console.WriteLine(domicilio.Calle);
                }

                IList results = new ArrayList();
                results = session.Find("SELECT Id, Apellido, Nombre FROM Persona WHERE Id = 1");
                foreach (var renglon in results)
                {
                    Console.WriteLine(((object[])(renglon))[0]);
                    Console.WriteLine(((object[])(renglon))[1]);
                    Console.WriteLine(((object[])(renglon))[2]);
                }
            }

            Console.ReadLine();
        }
    }
}
